package com.yjt.zdjf.standard.service.zdcx;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.yjt.common.util.TimeUtil;
import com.yjt.zdjf.standard.common.bean.domain.TxnZDCX;
import com.yjt.zdjf.standard.common.dao.ZDCXDao;
import com.yjt.zdjf.standard.communicate.socket.bank.server.ZDJFTcpMessageSender;
import com.yjt.zdjf.standard.model.ZDJFMessage;
import com.yjt.zdjf.standard.model.common.Head;
import com.yjt.zdjf.standard.model.zdcx.ZDCXReqMessage;
import com.yjt.zdjf.standard.model.zdcx.ZDCXRspMessage;
import com.yjt.zdjf.standard.service.AbstractStandarService;

@Service(ZDCXService.PACKAGE_CLASS_NAME)
public class ZDCXService extends AbstractStandarService<ZDCXReqMessage, ZDCXRspMessage, TxnZDCX> {

	private static final Logger logger = LoggerFactory.getLogger(ZDCXService.class);
	
	public static final String PACKAGE_CLASS_NAME = "com.yjt.zdjf.standard.service.zdcx.ZDCXService";
	
	@Resource(name = ZDCXDao.PACKAGE_CLASS_NAME)
	private ZDCXDao zdcxDao;
	
	@Resource(name = "zdjfStandardCommunicateSocketBankClientMessageSender")
	private ZDJFTcpMessageSender zdjfTcpMessageSender;
	
	public TxnZDCX msgConvertToDomain(ZDCXReqMessage request) throws Exception {
		logger.info("[账单查询]格式转换{innerMsgId:{}}", request.getHead().getInnerMsgId());
		TxnZDCX domain = new TxnZDCX();
		domain.setInnerMsgId(request.getHead().getInnerMsgId());
		domain.setOuterMsgId(request.getHead().getOuterMsgId());
		return domain;
	}
	
	public boolean checkFieldFormat(TxnZDCX txnZDCX) {
		boolean validate = true;
		logger.info("[账单查询]格式校验{innerMsgId:{}}", txnZDCX.getInnerMsgId());
		return validate;
	}
	
	public boolean checkBussinessElement(TxnZDCX txnZDCX) {
		boolean validate = true;
		logger.info("[账单查询]业务要素校验{innerMsgId:{}}", txnZDCX.getInnerMsgId());
		return validate;
	}
	
	/**
	 * @param request
	 * @return 
	 */
	public void makePersistent(TxnZDCX txnZDCX) {
		logger.info("[账单查询]持久化{innerMsgId:{}}", txnZDCX.getInnerMsgId());
	}
	
	/**
	 * 处理请求过程中发生错误
	 */
	public ZDCXRspMessage processError(ZDCXReqMessage request, Throwable cause) {
		//1.先根据innerMsgId找到对应的记录
		//2.若存在，则更新为失败，更新错误码和错误码说明
		//3.最终回应失败
		String innerMsgId = request.getHead().getInnerMsgId();
		logger.error(ToStringBuilder.reflectionToString(request), cause);
		ZDCXRspMessage rsp = new ZDCXRspMessage();
		return rsp;
	}

	@Override
	public ZDCXReqMessage assembleReq(TxnZDCX txnObject)
			throws Exception {
		logger.info("[账单查询]组装报文{innerMsgId:{}}", txnObject.getInnerMsgId());
		ZDCXReqMessage message = new ZDCXReqMessage();
		message.getBody().setByzd("备用字段");
		message.getBody().setJfh("缴费号");
		message.getBody().setJszdzq("结算账单周期");
		message.getBody().setQszdzq("清算账单周期");
		message.getBody().setQyh("签约");
		Head head = message.getHead();
		head.setOuterMsgId("12344");
		head.setRecSysId("0001");
		head.setSendDate(TimeUtil.getCurDate("yyyyMMdd"));
		head.setSendTime(TimeUtil.getCurDate("HHmmss"));
		head.setTxnType("zdcx");
		head.setInnerMsgId(txnObject.getInnerMsgId());
		return message;
	}

	@Override
	public ZDCXRspMessage processRsp(ZDCXReqMessage request) throws Exception {
		logger.info("[账单查询]发送报文到银行端{innerMsgId:{}}", request.getHead().getInnerMsgId());
		Future<ZDJFMessage> futureTask = zdjfTcpMessageSender.send(request);
		ZDJFMessage rsp = futureTask.get(300000, TimeUnit.SECONDS);
		System.out.println("================ rsp : ");
		System.out.println(JSON.toJSONString(rsp));
		return (ZDCXRspMessage)rsp;
	}
}
